package com.ihighest.app.infox.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.ihighest.app.infox.R;
import com.ihighest.app.infox.adapter.MyGridAdapter;
import com.ihighest.app.infox.bean.ImageItem;
import com.ihighest.app.infox.tools.ReturnItemImage;

public class ImageshowActivity extends Activity {
	private TextView textView;
	private Button button;
	private GridView gridView;
	private List<ImageItem> list;
	private List<ImageItem> setlist = new ArrayList<ImageItem>();
	private int maxcount;

	private int count = 0;
	MyGridAdapter adapter;
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			list = (List<ImageItem>) msg.obj;
			adapter = new MyGridAdapter(getApplicationContext(), list, gridView, 90);
			gridView.setAdapter(adapter);
			gridView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
					ImageItem str = list.get(position);

					if (str.type) {
						str.type = false;
						for (int i = 0; i < setlist.size(); i++) {
							if (setlist.get(i).path.equals(list.get(position).path)) {
								setlist.remove(i);
								count = count - 1;

							}
						}
					} else {
						if (count < maxcount) {
							str.type = true;
							setlist.add(list.get(position));
							count = count + 1;
						} else {
							Toast.makeText(getApplicationContext(), "最多选择6张照片", Toast.LENGTH_SHORT).show();
						}
					}
					list.get(position).type = str.type;
					adapter.notifyDataSetChanged();
					int i = maxcount - count;
					if(i<0){
						i=0;
					}
					textView.setText("还可以选择" + i + "张");

				}
			});
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.infox_imageshow);
		textView = (TextView) findViewById(R.id.image_tishi);
		button = (Button) findViewById(R.id.image_button);
		gridView = (GridView) findViewById(R.id.image_gridview);
		Intent getIntent = getIntent();
		maxcount = getIntent.getIntExtra("max", 0);
		new ReturnItemImage(this, handler).readimage();
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				wancheng();

			}
		});

	}

	private void wancheng() {
		Intent intent = new Intent();
		if (setlist.size() > 0) {
			String str = "";
			for (int i = 0; i < setlist.size(); i++) {
				str += setlist.get(i).path + ";";
			}
			if (!str.equals("")) {
				str = str.substring(0, str.length() - 1);
			}
			intent.putExtra("image", str);
		}
		setResult(100, intent);
		finish();
	}

}
